#include"WfRestSer.h"
#include<iostream>
#include"nlohmann/json.hpp"

using std::cout;

WfRestSer::WfRestSer(WebPageQuery* query, KeyRecommander* key)
    :_httpserver()
    ,_keyrecon(key)
    ,_wordquery(query)
{


}

    bool WfRestSer::start(int port )
{
    initHttpServer();
    int ret =  _httpserver.track().start(port);
    if(ret==0)
    {
        _httpserver.list_routes();
        return true;
    }
    return false;
}

void WfRestSer::stop()
{
    cout<<" finish\n";
    _httpserver.stop();
}


    void WfRestSer::initHttpServer()
{
    _httpserver.GET("/start",[](const wfrest::HttpReq* req, wfrest::HttpResp* resp)
                    {
                    cout<<"launch \n";
                    resp->File("/home/sakura/all_file/search_engine/static/mainpage.html");
                    });

    _httpserver.GET("/recommand",[this](const wfrest::HttpReq * req ,wfrest::HttpResp* resp)
    {
        cout<<" will push recommand\n";
    });
    _httpserver.POST("/recommand",[this](const wfrest::HttpReq * req ,wfrest::HttpResp* resp)
                     {
                     cout<<"need recommand\n";
                     string raw = req->body();
                     cout<<" raw  "<<raw<<"\n";
                     nlohmann::json json =  nlohmann::json::parse(raw);
                     string rec;
                     for(auto&i:json.items())
                     {
                        rec = i.value();
                     }
                     cout<<rec<<"\n";

                    nlohmann::json  rep=_keyrecon->recommand(rec);
                    cout<<rep.dump()<<"\n";
                     resp->append_output_body(rep.dump());
                     });

    _httpserver.GET("/search",[this](const wfrest::HttpReq* req ,wfrest::HttpResp* resp)
                    {
                    cout<<"search\n";
                    resp->File("/home/sakura/all_file/search_engine/static/search.html");
                    });

    _httpserver.POST("/search",[this](const wfrest::HttpReq * req ,wfrest::HttpResp* resp)
                     {
                     cout<<"need search\n";
                      map<string,string>& bodyMap =req->form_kv();
                      nlohmann::json rep;
                     rep=_wordquery->doQuery(bodyMap["key"]);
                     resp->append_output_body(rep.dump());
                     });

    _httpserver.GET("/styles.css",[this](const wfrest::HttpReq* req ,wfrest::HttpResp* resp)
                    {
                    cout<<"get realted file\n";
                    resp->File("/home/sakura/all_file/search_engine/static/styles.css");
                    });
    _httpserver.GET("/script.js",[this](const wfrest::HttpReq* req ,wfrest::HttpResp* resp)
                    {
                    cout<<"get realted file\n";
                    resp->File("/home/sakura/all_file/search_engine/static/script.js");
                    });
    _httpserver.GET("/favicon.js",[this](const wfrest::HttpReq* req ,wfrest::HttpResp* resp)
                    {
                    cout<<"get realted file\n";
                    resp->File("/home/sakura/all_file/search_engine/static/script.js");
                    });
    
    
    
}

